package com.wanfajie.smsdistribute.common.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.wanfajie.smsdistribute.R

class SlideIndexView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    companion object {
        const val DEFAULT_ACTIVE_TEXT_COLOR = Color.RED
        const val DEFAULT_TEXT_COLOR = Color.BLACK
        const val DEFAULT_BG_COLOR = Color.TRANSPARENT
        const val DEFAULT_SLIDE_BG_COLOR = 0xffcccccc.toInt()
        const val DEFAULT_TEXT_SIZE = 24f
    }

    private var mTextColor = DEFAULT_TEXT_COLOR
    private var mActiveTextColor = DEFAULT_ACTIVE_TEXT_COLOR
    private var mBgColor = DEFAULT_BG_COLOR
    private var mSlideBackgroundColor = DEFAULT_SLIDE_BG_COLOR
    private var mTextSize = DEFAULT_TEXT_SIZE
    private var mItems: Array<CharSequence> = arrayOf(
            "#", "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z")

    private var mCurrentPos = -1
    private val mPaint: Paint = Paint()

    private var mTVDialog: TextView? = null
    private var mItemSlideListener: OnItemSlideListener? = null

    init {
        mPaint.isAntiAlias = true
        attrs?.let {
            loadCustomAttr(it, defStyleAttr)
        }
        mPaint.textSize = mTextSize
        setBackgroundColor(mBgColor)
    }

    fun setTextViewDialog(textView: TextView?) {
        mTVDialog = textView
    }

    private fun loadCustomAttr(attrs: AttributeSet, defStyleAttr: Int) {
        val ta = context.theme.obtainStyledAttributes(attrs, R.styleable.SlideIndexView, defStyleAttr, 0)
        mTextColor = ta.getColor(
                R.styleable.SlideIndexView_textColor,
                DEFAULT_TEXT_COLOR)

        mActiveTextColor = ta.getColor(
                R.styleable.SlideIndexView_activeTextColor,
                DEFAULT_ACTIVE_TEXT_COLOR)

        mBgColor = ta.getColor(
                R.styleable.SlideIndexView_backgroundColor,
                DEFAULT_BG_COLOR)

        mSlideBackgroundColor = ta.getColor(
                R.styleable.SlideIndexView_slideBackgroundColor,
                DEFAULT_SLIDE_BG_COLOR)

        mTextSize = ta.getDimension(
                R.styleable.SlideIndexView_textSize,
                DEFAULT_TEXT_SIZE)

        mItems = ta.getTextArray(R.styleable.SlideIndexView_entries)
    }

    override fun onDraw(canvas: Canvas) {
        for (i in mItems.indices) {
            mPaint.color = if (i == mCurrentPos) {
                mActiveTextColor
            } else {
                mTextColor
            }

            val item = mItems[i]
            canvas.drawText(
                    item.toString(),
                    (width - mPaint.measureText(item.toString())) / 2,
                    (i + 1.0f) * height / mItems.size,
                    mPaint
            )
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val perTextHeight = height / mItems.size
        val y = event.y
        val currentPosition = (y / perTextHeight).toInt()

        when (event.action) {
            MotionEvent.ACTION_UP -> {
                setBackgroundColor(mBgColor)
                mTVDialog?.let {
                    it.visibility = View.GONE
                }
                invalidate()
            }
            MotionEvent.ACTION_DOWN,
            MotionEvent.ACTION_MOVE -> {
                setBackgroundColor(mSlideBackgroundColor)
                if (currentPosition > -1 && currentPosition < mItems.size) {
                    val letter = mItems[currentPosition]
                    mTVDialog?.let {
                        it.visibility = View.VISIBLE
                        it.text = letter
                    }
                    mItemSlideListener?.onItemSlide(letter)

                    mCurrentPos = currentPosition
                }
                invalidate()
            }
        }

        return true
    }

    fun setOnItemSlideListener(listener: OnItemSlideListener?) {
        mItemSlideListener = listener
    }

    fun setOnItemSlideListener(listener: ((CharSequence) -> Unit)?) {
        if (listener == null) {
            mItemSlideListener = null
        } else {

            setOnItemSlideListener(object : OnItemSlideListener {
                override fun onItemSlide(item: CharSequence) {
                    listener.invoke(item)
                }
            })
        }
    }

    interface OnItemSlideListener {
        fun onItemSlide(item: CharSequence)
    }
}